package com.example.wx.dailyreport.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wx.dailyreport.customtool.WordUtil;
import com.example.wx.dailyreport.pojo.Dailyreport;
import com.example.wx.dailyreport.service.DailyreportService;
import com.example.wx.dailyreport.service.ReportService;
import com.example.wx.dailyreport.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hulinbo
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/dailyreport")
public class DailyreportController {

    public static final String CONTENTSPLIT = "!@#\\$%\\^&\\*!";
    public static final String KONG = "_#!_$@";
    public static final String PATH = "D:/mywordfile/";
    public static final String FILEENDNAME = ".doc";

    @Autowired
    private DailyreportService dailyreportService;

    @Autowired
    private UserService userService;

    @Autowired
    private ReportService reportService;

    @PostMapping("/save")
    public Map<String, Object> saveDailyreport(Dailyreport dailyreport, String report) {
        Map<String, Object> map = new LinkedHashMap<>();
        dailyreport.setUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        if (dailyreportService.saveDailyreport(dailyreport, report))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @GetMapping("/getselfdailyreport")
    public Map<String, Object> getDailyreport(Dailyreport dailyreport, String start, String end, String reportcontent, @RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new LinkedHashMap<>();
        dailyreport.setUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        IPage<Dailyreport> iPage = dailyreportService.selectDailyreport(dailyreport, start, end, reportcontent, page, limit);
        map.put("status", 200);
        map.put("data", iPage.getRecords());
        map.put("count", iPage.getTotal());
        return map;
    }

    @GetMapping("/getadminreport")
    public Map<String, Object> getAdminReport(Dailyreport dailyreport, String start, String end, String reportcontent, @RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new LinkedHashMap<>();
        dailyreport.setUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        IPage<Dailyreport> iPage = dailyreportService.selectAdminReport(dailyreport, start, end, reportcontent, page, limit);
        map.put("status", 200);
        map.put("data", iPage.getRecords());
        map.put("count", iPage.getTotal());
        return map;
    }

    @PostMapping("/checkreport")
    public Map<String, Object> checkReport(Integer id) {
        Map<String, Object> map = new LinkedHashMap<>();
        Dailyreport dailyreport = new Dailyreport();
        dailyreport.setId(id);
        dailyreport.setStatus(2);
        if (dailyreportService.updateById(dailyreport))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @PostMapping("/downloadword")
    public Map<String, Object> downloadWord(@RequestBody ArrayList<Dailyreport> dailyreports) throws Exception {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Dailyreport d : dailyreports) {
            Map<String, Object> dataMap = new HashMap<>();
            d.setUsername(userService.findByUsername(d.getUsername()).getName());
            if (d.getSubmittime() == null) {
                d.setSubmittime(d.getCreatetime());
            }
            dataMap.put("dailyreport", d);
            String[] split = reportService.getById(d.getDailyId()).getReportcontent().split(DailyreportController.CONTENTSPLIT);
            for (int i = 0; i < split.length; i++) {
                if (KONG.equals(split[i]))
                    split[i] = " ";
            }
            dataMap.put("report", split);
            WordUtil.generateWord(dataMap, PATH + d.getUsername() + System.currentTimeMillis() + FILEENDNAME);
        }
        map.put("status", 200);
        return map;
    }


}

